package mcp

import (
	"context"
	"github.com/mark3labs/mcp-go/mcp"
	"go.uber.org/zap"
	"mcp-demo/internal/global"
	"mcp-demo/internal/model/vo/common/result"
	"mcp-demo/internal/model/vo/intention/resp"
	"mcp-demo/internal/utils/jsonutil"
)

func init() {
	RegisterTool(&Query{})
}

// Query 保存问题
type Query struct {
}

// Handle 保存用户查询
func (qs *Query) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	// 调用业务层, 调用持久层, 查询数据库
	arguments := request.GetArguments()
	// 如果是必填参数可以加校验
	p1, ok := arguments["param1"].(string)
	if !ok {
		p1 = ""
	}
	p2, ok := arguments["param2"].(string)
	if !ok {
		p2 = ""
	}
	demoRes, err := intentionService.QueryDemo(p1, p2)
	var jsonString string
	if err != nil {
		global.L.Error(err.Error(), zap.Error(err))
		jsonString, _ = jsonutil.GetJsonString(result.FailResult())
	} else {
		jsonString, _ = jsonutil.GetJsonString(result.OkResult[[]resp.DemoResp](demoRes))
	}
	return &mcp.CallToolResult{
		Content: []mcp.Content{
			mcp.TextContent{
				Type: "text",
				Text: jsonString,
			},
		},
	}, nil
}

// New 保存用户查询描述
func (qs *Query) New() mcp.Tool {
	return mcp.NewTool("query",
		mcp.WithDescription("示例查询"),
		mcp.WithString("param1",
			mcp.Required(),
			mcp.Description("测试问题1"),
		),
		mcp.WithString("param2",
			mcp.Description("测试问题2"),
		),
	)
}
